var pnum = "";   
var initData = function(num){
		$.ajax({
	        url: base + "/dataCenter/businessdata/findContentMatchingMentions",
	        data: {},
	        success: function (data, textStatus, jqXHR) {
	        	var dataObj ={
	        			"list":data,
	         			"dataTool":{
	         				"title":{
	          			      	"project_design_content_code": "设计内容代码"
	          			      	,"project_design_content_name": "设计内容名称"
	          			      	,"project_design_phase_name": "设计阶段"
	          			      	,"project_design_major_name": "相关专业"
	          			      	,"mention_information_name" : "相关资料"
	            			},
	            			"titleCheckbox":false
						}
		  		};
                showStaticPageTable('view', dataObj,"10");
	        },
	        error: function () {
	        }
	    });
  }

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           content_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findContentMatchingMentions",
           data: param,
           success: function (data, textStatus, jqXHR) {
               var dataObj ={
                   "list":data,
                   "dataTool":{
                       "title":{
                           "project_design_content_code": "设计内容代码"
                           ,"project_design_content_name": "设计内容名称"
                           ,"project_design_phase_name": "设计阶段"
                           ,"project_design_major_name": "相关专业"
                           ,"mention_information_name" : "相关资料"
                       },
                       "titleCheckbox":false
                   }
               };
               showStaticPageTable('view', dataObj,"10");
           },
           error: function () {
           }
       });
   }

   var addData = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "content_id":"设计内容",
					      	"major_id":"所属专业",
					      	"phase_id":"请选择项目阶段",
					      	"mention_infos":"请选择资料信息"
					},"text":{
						    "content_id":""
						    ,"major_id":""
						    ,"phase_id":""
							,"mention_infos":""
					},"input":{
						    "content_id":"_select"
							,"major_id":"select"
							,"phase_id":"new_select"
						    ,"mention_infos":"checkbox"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		initDesignContent();
		chanageContentselect();//监听设计内容下拉
		initPhase();//初始化设计阶段
		chanageMajorselect();//监听专业下拉选
		chanagePhaseselect();//监听阶段下拉选
		layopen(contentDiv());//渲染到弹窗
	
   }
   
   var initDesignContent = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignContent",{},
	   		  	function(data,status){
   		    	if(data != null && data.list != null){
 	      		  	 var optionstring = "";
 	                 $.each(data.list, function(i,item){
 	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
 	                });
 	                $("#_select").html('<option value=""></option>' + optionstring);
 	      		}else{
 	      		  	$("#_select").html('<option value=""></option>');
 	      		}
   		    form.render(); 
	  });
   }
   
   
   var initPhase = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
	   		  	function(data,status){
    		    	if(data != null && data.list != null){
  	      		  	 var optionstring = "";
  	                 $.each(data.list, function(i,item){
  	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
  	                });
  	                $("#new_select").html('<option value=""></option>' + optionstring);
  	      		}else{
  	      		  	$("#new_select").html('<option value=""></option>');
  	      		}
    		    form.render(); 
	  });
   }
   
   var initMajorDate = function(content_id){
	   $.post(base+"/dataCenter/businessdata/findMatchingMajorList",{content_id:content_id},
	   		  	function(data,status){
     		    	if(data!=''){
   	      		  	 var optionstring = "";
   	                 $.each(data, function(i,item){
   	                	if(item.content_id != null && item.content_id != ""){
   	                	 optionstring += "<option value=\"" + item.major_id + "\" >" + item.major_name + "</option>";
   	                	}
   	                });
   	                $("#select").html('<option value=""></option>' + optionstring);
   	      		}else{
   	      		  	$("#select").html('<option value=""></option>');
   	      		}
     		    form.render(); 
	  });
   }
   
	   var layopen = function(content){
			layer.open({
			  	title:'匹配专业资料信息'
				,content: content
		    	,area: ['800px', '600px']
				,scrollbar: false			
			  	,btn: ['确认', '取消']
				,yes: function(index,layero){ //确认
					var data = $(layero).find("form").serializeJSON();
					var content_id = data.content_id;
					var phase_id = data.phase_id;
					var major_id = data.major_id;
					var mention_arr = new Array();
					$.each($('input[type=checkbox]:checked'),function(index,value){
						mention_arr.push($(this).attr("name"));
						});
					if(mention_arr.length < 1){
						alert("请选择专业资料！")
					    return false;
					}
					var mention_str = mention_arr.join(",");
					//提交
					commitData(content_id,phase_id,major_id,mention_str);
				}
		});
	 }
	   
	  var commitData = function(content_id,phase_id,major_id,mention_str){
			$.post(base+"/dataCenter/businessdata/addMentionsByDesignContent",{
				content_id:content_id,
				phase_id:phase_id,
				major_id:major_id,
				mention_str:mention_str
			},
	  		function (data,status){
				if(data!=""&&data!=null){
					if(data.statusCode == 0){
						layer.msg('更新成功', {icon: 1,time:2000});
					}else{
						layer.msg(data.message, {icon: 2,time:2000});
					}
				}else{
					layer.msg('更新失败!', {icon: 2,time:2000});
				}
				initData(pnum);
	  		 })
	   }
	   
	  var chanageContentselect = function(){
	  		form.on('select(_select)',function(value){
	  			var new_content_id = value.value;
	  			//加载专业
	  			initMajorDate(new_content_id);//初始化专业
	  		});
	  }
	  
	  var chanageMajorselect = function(){
	  		   form.on('select(select)',function(value){
	  			var major_id = value.value;
	  			var con_id = $("#_select option:selected").val();
	  			var phase_id=$("#new_select option:selected").val();
	  			if(phase_id == "" || phase_id == null || con_id == "" || con_id == null){
	  				return false;
	  			}
	  			//加载资料
	  			initMentions(major_id,con_id,phase_id);
	  		});
	  }
	  
	  var chanagePhaseselect = function(){
	  		form.on('select(new_select)',function(value){
	  			var phase_id = value.value;
	  			var con_id = $("#_select option:selected").val();
	  			var major_id=$("#select option:selected").val();
	  			if(major_id == "" || major_id == null || con_id == "" || con_id == null){
	  				return false;
	  			}
	  			//加载资料
	  			initMentions(major_id,con_id,phase_id);
	  		});
	  }
	  
	  var initMentions = function(major_id , content_id , phase_id){
		  if(major_id == "" || content_id == "" || phase_id == ""){
			  return false;
		  }
		   $.post(base+"/dataCenter/businessdata/findMentionsByCondition",{major_id:major_id,content_id:content_id,phase_id:phase_id},
		   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var checkboxString = "";
	   	                 $.each(data, function(i,item){
   	                		if(item.record_id != null && item.record_id != ""){
   	                			checkboxString += " <input name = "+item.id+" title = "+item.mention_name+" type='checkbox' checked='' >";
   	                		}else{
   	                			checkboxString += " <input name = "+item.id+" title = "+item.mention_name+" type='checkbox'>";
   	                		}
	   	                });
		   	                $("#checkbox").html(checkboxString);
		   	      		}else{
		   	      		  	$("#checkbox").html("");
		   	      		}
		     		    form.render(); 
		   });
		  
	  }
	  
	   $(document).ready(function(){
		   initData(1);
	       $("#searchInfo").click(searchObj);
	       $("#addData").click(addData);
	   });

   